package com.aoyy.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import com.aoyy.model.BoardManager;
import com.aoyy.model.User;
import com.aoyy.model.Board;
import com.aoyy.service.BoardManagerService;
import com.aoyy.service.UserService;
import com.aoyy.service.BoardService;
import com.aoyy.util.Page;
import com.aoyy.constant.Constants;
/**=========================================================================================
 * Board manager controller <code>BoardManagerController</code>
 * @author this author
 * @date 2017-08-12
 * ========================================================================================= */
@Controller
public class BoardManagerController {
	@Autowired
	private BoardManagerService boardManagerService;
	@Autowired
	private UserService userService;
	@Autowired
	private BoardService boardService;
	private Integer pageNo;
	
	// To board manager list
	@RequestMapping(value="/boardManager/boardManagerList", method={RequestMethod.GET})
	public String toBoardManagerList(HttpServletRequest request, @RequestParam(value="pageNo", required=false) Integer pageNo) {
		List<User> userList = userService.queryAllUsers();
		request.setAttribute("userList", userList);
		pageNo = (pageNo == null) ? 1 : pageNo;
		Page pagedBoards = boardService.pagedQuery(pageNo, Constants.PAGE_SIZE);
		request.setAttribute("pagedBoards", pagedBoards);
		request.setAttribute("page", "page1");
		return "/boardManager/boardManagerList";
	}
	
	// To board manager list by userId
	@RequestMapping(value="/boardManager/boardManagerList-{userId}", method={RequestMethod.GET})
	public String toBoardManagerByUserId(HttpServletRequest request, @RequestParam(value="pageNo", required=false) Integer pageNo, @PathVariable Integer userId) {
		List<User> userList = userService.queryAllUsers();
		request.setAttribute("userList", userList);
		pageNo = (pageNo == null) ? 1 : pageNo;
		Page pagedBoards = boardService.pagedQuery(pageNo, Constants.PAGE_SIZE);
		this.pageNo = pageNo;
		request.setAttribute("pagedBoards", pagedBoards);
		List<BoardManager> boardManagers = boardManagerService.queryBoardManagersByUserId(userId);
		request.setAttribute("boardManagers", boardManagers);
		request.setAttribute("userId", userId);
		request.setAttribute("page", "page2");
		return "/boardManager/boardManagerList";
	}
	
	// Save board manager by checked, userId, and boardId
	@RequestMapping(value="/boardManager/boardManagerList-{checkedUserIdBoardId}", method={RequestMethod.POST})
	public String toBoardManagerByCheckedUserIdBoardId(HttpServletRequest request, @RequestParam(value="pageNo", required=false) Integer pageNo, @PathVariable String checkedUserIdBoardId) {
		String[] str = checkedUserIdBoardId.split("-");
		Integer checked = Integer.valueOf(str[0]);
		Integer userId = Integer.valueOf(str[1]);
		Integer boardId = Integer.valueOf(str[2]);
		System.out.println(checkedUserIdBoardId);
		System.out.println("checked: " + checked + ", userId: " + userId + ", boardId: " + boardId);
		if (checked == 1) {
			boardManagerService.delete(boardId, userId);
		} else if (checked == 0) {
			BoardManager boardManager = new BoardManager();
			Board board = new Board();
			board.setBoardId(boardId);
			boardManager.setBoard(board);
			User user = new User();
			user.setUserId(userId);
			boardManager.setUser(user);
			boardManagerService.save(boardManager);
		}
		List<User> userList = userService.queryAllUsers();
		request.setAttribute("userList", userList);
		// pageNo = (pageNo == null) ? 1 : pageNo;
		pageNo = (pageNo == null) ? this.pageNo : pageNo;
		Page pagedBoards = boardService.pagedQuery(pageNo, Constants.PAGE_SIZE);
		request.setAttribute("pagedBoards", pagedBoards);
		List<BoardManager> boardManagers = boardManagerService.queryBoardManagersByUserId(userId);
		request.setAttribute("boardManagers", boardManagers);
		request.setAttribute("userId", userId);
		request.setAttribute("checkedUserIdBoardId", checkedUserIdBoardId);
		request.setAttribute("page", "page3");
		return "/boardManager/boardManagerList";
	}

}
